package com.ujs.order.controller;


import com.ujs.commonutils.constant.R;
import com.ujs.commonutils.utils.JwtUtils;
import com.ujs.order.entity.TOrder;
import com.ujs.order.service.TOrderService;
import com.ujs.servicebase.exception.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-09-03
 */
@RestController
@RequestMapping("/eduorder/torder")
@Api(tags = "订单api")
public class TOrderController {
    @Autowired
    private TOrderService orderService;

    @ApiOperation("生成订单，返回订单号")
    @GetMapping("/createOrder/{courseId}")
    public R saveOrder(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (!StringUtils.isEmpty(memberId)) {
            String orderSn = orderService.saveOrder(courseId, memberId);
            return R.ok().data("orderSn", orderSn);
        } else {
            return R.error().message("请先登录");
        }
    }

    @ApiOperation("根据订单号，返回订单的信息")
    @GetMapping("/getOrderInfoById/{orderNo}")
    public R getOrderInfoByOrderNo(@PathVariable("orderNo") String orderNo) {
        TOrder order = orderService.getOrderInfo(orderNo);
        return R.ok().data("order", order);
    }

    @ApiOperation("根据课程id和用户id查询订单表中订单状态")
    @GetMapping("/isBuyCourse/{courseId}/{memberId}")
    public Boolean getOrderStatus(@PathVariable("courseId") String courseId, @PathVariable("memberId") String memberId) {
        return orderService.isBuyCourse(courseId, memberId);
    }
}

